<script setup lang="ts">
import { onMounted, reactive, toRefs, onBeforeUnmount } from "vue";
import { getMonitorServerList } from "@/api/monitorServer";
import VChart from "./components/VChart.vue";
const options = reactive({
  show: false,
  monitor: null,
  url: "api/monitor",
  data: {},
  cpuInfo: {
    animation: true,
    tooltip: {
      trigger: "axis"
    },
    xAxis: {
      type: "category",
      boundaryGap: false,
      data: []
    },
    yAxis: {
      type: "value",
      min: 0,
      max: 100,
      interval: 20
    },
    series: [
      {
        name: "CPU使用率",
        data: [],
        type: "line",
        areaStyle: {
          normal: {
            color: "rgb(32, 160, 255)" // 改变区域颜色
          }
        },
        itemStyle: {
          normal: {
            color: "#6fbae1",
            lineStyle: {
              color: "#6fbae1" // 改变折线颜色
            }
          }
        }
      }
    ]
  },
  memoryInfo: {
    tooltip: {
      trigger: "axis"
    },
    xAxis: {
      type: "category",
      boundaryGap: false,
      data: []
    },
    yAxis: {
      type: "value",
      min: 0,
      max: 100,
      interval: 20
    },
    series: [
      {
        name: "内存使用率",
        data: [],
        type: "line",
        areaStyle: {
          normal: {
            color: "rgb(32, 160, 255)" // 改变区域颜色
          }
        },
        itemStyle: {
          normal: {
            color: "#6fbae1",
            lineStyle: {
              color: "#6fbae1" // 改变折线颜色
            }
          }
        }
      }
    ]
  }
});
const { show, monitor, url, data, cpuInfo, memoryInfo } = toRefs(options);
const init = async () => {
  const res = await getMonitorServerList();
  data.value = res;
  show.value = true;
  if (cpuInfo.value.xAxis.data.length >= 8) {
    cpuInfo.value.xAxis.data.shift();
    memoryInfo.value.xAxis.data.shift();
    cpuInfo.value.series[0].data.shift();
    memoryInfo.value.series[0].data.shift();
  }
  cpuInfo.value.xAxis.data.push(res.time);
  memoryInfo.value.xAxis.data.push(res.time);
  cpuInfo.value.series[0].data.push(parseFloat(res.cpu.used));
  memoryInfo.value.series[0].data.push(parseFloat(res.memory.usageRate));
};
onMounted(() => {
  init();
  monitor.value = window.setInterval(() => {
    setTimeout(() => {
      init();
    }, 2);
  }, 3500);
});
onBeforeUnmount(() => {
  clearInterval(monitor.value);
});
</script>

<template>
  <div
    v-loading="!show"
    element-loading-text="数据加载中..."
    :style="!show ? 'height: 500px' : 'height: 100%'"
    class="app-container w-full"
  >
    <div v-if="show" class="w-full">
      <el-card class="box-card first w-full">
        <div style="font-size: 13px; color: #666">
          <svg-icon icon-class="system" style="margin-right: 5px" />
          <span> 系统：{{ data.sys.os }} </span>
          <span> IP：{{ data.sys.ip }} </span>
          <span> 项目已不间断运行：{{ data.sys.day }} </span>
          <i class="el-icon-refresh" style="margin-left: 40px" @click="init" />
        </div>
      </el-card>
      <el-card class="box-card w-full">
        <template v-slot:header>
          <div class="clearfix">
            <span style="font-size: 15px; font-weight: bold; color: #666"
              >状态</span
            >
          </div>
        </template>
        <div>
          <el-row>
            <el-col
              :xs="24"
              :sm="24"
              :md="6"
              :lg="6"
              :xl="6"
              style="margin-bottom: 10px"
            >
              <div class="title">CPU使用率</div>
              <el-tooltip placement="top-end">
                <template v-slot:content>
                  <div style="font-size: 12px">
                    <div style="padding: 3px">
                      {{ data.cpu.name }}
                    </div>
                    <div style="padding: 3px">
                      {{ data.cpu.package }}
                    </div>
                    <div style="padding: 3px">
                      {{ data.cpu.core }}
                    </div>
                    <div style="padding: 3px">
                      {{ data.cpu.logic }}
                    </div>
                  </div>
                </template>
                <div class="content">
                  <el-progress
                    type="dashboard"
                    :percentage="parseFloat(data.cpu.used)"
                  />
                </div>
              </el-tooltip>
              <div class="footer">{{ data.cpu.coreNumber }} 核心</div>
            </el-col>
            <el-col
              :xs="24"
              :sm="24"
              :md="6"
              :lg="6"
              :xl="6"
              style="margin-bottom: 10px"
            >
              <div class="title">内存使用率</div>
              <el-tooltip placement="top-end">
                <template v-slot:content>
                  <div style="font-size: 12px">
                    <div style="padding: 3px">
                      总量：{{ data.memory.total }}
                    </div>
                    <div style="padding: 3px">
                      已使用：{{ data.memory.used }}
                    </div>
                    <div style="padding: 3px">
                      空闲：{{ data.memory.available }}
                    </div>
                  </div>
                </template>
                <div class="content">
                  <el-progress
                    type="dashboard"
                    :percentage="parseFloat(data.memory.usageRate)"
                  />
                </div>
              </el-tooltip>
              <div class="footer">
                {{ data.memory.used }} / {{ data.memory.total }}
              </div>
            </el-col>
            <el-col
              :xs="24"
              :sm="24"
              :md="6"
              :lg="6"
              :xl="6"
              style="margin-bottom: 10px"
            >
              <div class="title">交换区使用率</div>
              <el-tooltip placement="top-end">
                <template v-slot:content>
                  <div style="font-size: 12px">
                    <div style="padding: 3px">总量：{{ data.swap.total }}</div>
                    <div style="padding: 3px">已使用：{{ data.swap.used }}</div>
                    <div style="padding: 3px">
                      空闲：{{ data.swap.available }}
                    </div>
                  </div>
                </template>
                <div class="content">
                  <el-progress
                    type="dashboard"
                    :percentage="parseFloat(data.swap.usageRate)"
                  />
                </div>
              </el-tooltip>
              <div class="footer">
                {{ data.swap.used }} / {{ data.swap.total }}
              </div>
            </el-col>
            <el-col
              :xs="24"
              :sm="24"
              :md="6"
              :lg="6"
              :xl="6"
              style="margin-bottom: 10px"
            >
              <div class="title">磁盘使用率</div>
              <div class="content">
                <el-tooltip placement="top-end">
                  <template v-slot:content>
                    <div style="font-size: 12px">
                      <div style="padding: 3px">
                        总量：{{ data.disk.total }}
                      </div>
                      <div style="padding: 3px">
                        空闲：{{ data.disk.available }}
                      </div>
                    </div>
                  </template>
                  <div class="content">
                    <el-progress
                      type="dashboard"
                      :percentage="parseFloat(data.disk.usageRate)"
                    />
                  </div>
                </el-tooltip>
              </div>
              <div class="footer">
                {{ data.disk.used }} / {{ data.disk.total }}
              </div>
            </el-col>
          </el-row>
        </div>
      </el-card>

      <div class="w-full">
        <el-row :gutter="6">
          <el-col
            :xs="24"
            :sm="24"
            :md="12"
            :lg="12"
            :xl="12"
            style="margin-bottom: 10px"
          >
            <el-card class="box-card">
              <template v-slot:header>
                <div class="clearfix">
                  <span style="font-size: 15px; font-weight: bold; color: #666"
                    >CPU使用率监控</span
                  >
                </div>
              </template>
              <div class="h-[400px]">
                <v-chart autoresize :options="cpuInfo" />
              </div>
            </el-card>
          </el-col>
          <el-col
            :xs="24"
            :sm="24"
            :md="12"
            :lg="12"
            :xl="12"
            style="margin-bottom: 10px"
          >
            <el-card class="box-card">
              <template v-slot:header>
                <div class="clearfix">
                  <span style="font-size: 15px; font-weight: bold; color: #666"
                    >内存使用率监控</span
                  >
                </div>
              </template>
              <div class="h-[400px]">
                <v-chart autoresize :options="memoryInfo" />
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
::v-deep .box-card {
  margin-bottom: 5px;

  .el-icon-refresh {
    float: right;
    margin-right: 10px;
    cursor: pointer;
  }
}

.first {
  span {
    margin-right: 28px;
  }
}

.cpu,
.memory,
.swap,
.disk {
  width: 20%;
  //float: left;
  padding-bottom: 20px;
  margin-right: 5%;
}

.title {
  margin-bottom: 16px;
  font-size: 15px;
  font-weight: 500;
  color: #999;
  text-align: center;
}

.footer {
  margin-top: -5px;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 500;
  color: #999;
  text-align: center;
}

.content {
  margin-top: 5px;
  margin-bottom: 5px;
  text-align: center;
}
</style>
